// Bogpakke.cpp : Implementation of CBogpakke
#include "stdafx.h"
#include "EBASdll.h"
#include "Bogpakke.h"
#include "EnumBogpakke.h"
#include "EnumBog.h"
#include "importado.h"

/////////////////////////////////////////////////////////////////////////////
// CBogpakke

STDMETHODIMP CBogpakke::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IBogpakke
	};
	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}
	return S_FALSE;
}

STDMETHODIMP CBogpakke::get_GUID(BSTR *pVal)
{
	*pVal = m_guid.copy();

	return S_OK;
}

STDMETHODIMP CBogpakke::get_Nummer(BSTR *pVal)
{
	*pVal = m_nummer.copy();

	return S_OK;
}

STDMETHODIMP CBogpakke::put_Nummer(BSTR newVal)
{
	m_nummer = newVal;

	return S_OK;
}

STDMETHODIMP CBogpakke::get_Navn(BSTR *pVal)
{
	*pVal = m_navn.copy();

	return S_OK;
}

STDMETHODIMP CBogpakke::put_Navn(BSTR newVal)
{
	m_navn = newVal;

	return S_OK;
}

STDMETHODIMP CBogpakke::Slet()
{
	_CommandPtr command;
	_RecordsetPtr recordset;

	const char sql[255] = "EBAS_SP_SLET_BOGPAKKE";

	try {
		command.CreateInstance(__uuidof(Command));		
		command->ActiveConnection = CDatabase::GetConnection();
		command->CommandText = sql;
		command->CommandType = adCmdStoredProc;
		command->Parameters->Append( command->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		command->Parameters->Append( command->CreateParameter("bogpakkeGuid", adVarChar, adParamInput, 255, m_guid) );
		
		recordset = command->Execute(NULL, NULL, adCmdStoredProc);
		if(recordset->ADO_EOF)
			return E_ACCESSDENIED;

		m_guid = bstr_t("");
		m_navn = bstr_t("");
		m_nummer = bstr_t("");

		if (recordset)
			recordset->Close();
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}
	return S_OK;
}

STDMETHODIMP CBogpakke::SetInfo()
{
	_CommandPtr command;
	_RecordsetPtr recordset;

	const char sql[255] = "EBAS_SP_GEM_BOGPAKKE";

	try {
		command.CreateInstance(__uuidof(Command));		
		command->ActiveConnection = CDatabase::GetConnection();
		command->CommandText = sql;
		command->CommandType = adCmdStoredProc;
		command->Parameters->Append( command->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		command->Parameters->Append( command->CreateParameter("nr", adVarChar, adParamInput, 255, m_nummer) );
		command->Parameters->Append( command->CreateParameter("navn", adVarChar, adParamInput, 255, m_navn) );
		
		recordset = command->Execute(NULL, NULL, adCmdStoredProc);
		if(recordset->ADO_EOF)
			return E_ACCESSDENIED;

		m_guid = (bstr_t)recordset->Fields->GetItem("GUID")->Value;
		
		if (recordset)
			recordset->Close();
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}
	return S_OK;
}

STDMETHODIMP CBogpakke::TilfoejBog(IBog *pBog)
{
	_CommandPtr command;
	_RecordsetPtr recordset;

	const char sql[255] = "EBAS_SP_TILMELD_BOG_BOGPAKKE";

	try {
		BSTR bog;
		pBog->get_GUID(&bog);

		command.CreateInstance(__uuidof(Command));		
		command->ActiveConnection = CDatabase::GetConnection();
		command->CommandText = sql;
		command->CommandType = adCmdStoredProc;
		command->Parameters->Append( command->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		command->Parameters->Append( command->CreateParameter("bogpakkeGuid", adVarChar, adParamInput, 255, m_guid) );
		command->Parameters->Append( command->CreateParameter("bogGuid", adVarChar, adParamInput, 255, bstr_t(bog)) );
		
		recordset = command->Execute(NULL, NULL, adCmdStoredProc);
		if(recordset->ADO_EOF)
			return E_ACCESSDENIED;
		
		if (recordset)
			recordset->Close();
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}
	return S_OK;
}

STDMETHODIMP CBogpakke::FjernBog(IBog *pBog)
{
	_CommandPtr command;
	_RecordsetPtr recordset;

	const char sql[255] = "EBAS_SP_AFMELD_BOG_BOGPAKKE";

	try {
		BSTR bog;
		pBog->get_GUID(&bog);

		command.CreateInstance(__uuidof(Command));		
		command->ActiveConnection = CDatabase::GetConnection();
		command->CommandText = sql;
		command->CommandType = adCmdStoredProc;
		command->Parameters->Append( command->CreateParameter("session", adVarChar, adParamInput, 255, gpSessionGuid) );
		command->Parameters->Append( command->CreateParameter("bogpakkeGuid", adVarChar, adParamInput, 255, m_guid) );
		command->Parameters->Append( command->CreateParameter("bogGuid", adVarChar, adParamInput, 255, bstr_t(bog)) );

		recordset = command->Execute(NULL, NULL, adCmdStoredProc);
		if(recordset->ADO_EOF)
			return E_ACCESSDENIED;
		
		if (recordset)
			recordset->Close();
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();
	}
	return S_OK;
}

STDMETHODIMP CBogpakke::get_Indhold(IEnumBog **pVal)
{
	try {
		CComObject<CEnumBog> * pEnumBog;
		CComObject<CEnumBog>::CreateInstance(&pEnumBog);
		pEnumBog->findBogBogpakke(m_guid);
		pEnumBog->QueryInterface(pVal);
	} catch (_com_error &e) {
		CErrorHandling::SetErrorMessage(e.ErrorMessage());
		return e.Error();		
	}

	return S_OK;
}
